package net.gamebookengine.viewer;

import java.util.List;

import net.gamebookengine.viewer.entities.Choice;
import net.gamebookengine.viewer.entities.Section;
import net.gamebookengine.viewer.helper.ThemeHelper;
import net.gamebookengine.viewer.helper.ToastHelper;
import roboguice.activity.RoboActivity;
import roboguice.inject.InjectView;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.inject.Inject;

public class SectionActivity extends RoboActivity
{
	@InjectView(R.id.section_scrollview) ScrollView	scrollView;
	@InjectView(R.id.section_root) LinearLayout	 	layout;
	@InjectView(R.id.text) TextView					text;
	@InjectView(R.id.choices) LinearLayout			choiceArea;

	@Inject ThemeHelper								themeHelper;
	@Inject IGamebookService						gamebookService;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_section);
		
		themeHelper.applyTheme(gamebookService.getTheme(), scrollView);
		
		Section section = gamebookService.getSection(StaticStorage.CurrentSectionId);
		
		String imageFileName = section.getImage();
		if (imageFileName != null)
		{
			ImageView image = new ImageView(this);
			Bitmap bitmap = gamebookService.getImage(imageFileName);
			image.setImageBitmap(bitmap);
			layout.addView(image, 0);
		}
		
		text.setText(section.getText());
		
		choiceArea.removeAllViews();
		List<Choice> choices = section.getChoices();
		if (choices != null)
		{
			for (Choice choice : choices)
			{
				Button newButton = new Button(this);
				newButton.setText(choice.getText());
				newButton.setOnClickListener(new ChoiceOnClickListener(choice.getTarget()));
				choiceArea.addView(newButton);
			}
		}
		else
		{
			Button newButton = new Button(this);
			newButton.setText("zum Deckblatt");
			newButton.setOnClickListener(new OnClickListener()
			{
				public void onClick(View v)
				{
					Intent intent = new Intent(SectionActivity.this, TitleActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					startActivity(intent);
				}
			});
			choiceArea.addView(newButton);
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		getMenuInflater().inflate(R.menu.activity_section, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		boolean result;
		
		switch (item.getItemId())
		{
			case R.id.menu_leave_gamebook:
			{
				Intent intent = new Intent(SectionActivity.this, MainActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
				result = true;
				break;
			}
			
			case R.id.menu_reset_gamebook:
			{
				Intent intent = new Intent(SectionActivity.this, TitleActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
				result = true;
				break;
			}
			
			case R.id.menu_settings:
			{
				startActivity(new Intent(SectionActivity.this, SettingsActivity.class));
				result = true;
				break;
			}
			
			case R.id.menu_about:
			{
				startActivity(new Intent(SectionActivity.this, AboutActivity.class));
				result = true;
				break;
			}
			
			default:
			{
				result = super.onOptionsItemSelected(item);
				break;
			}
		}
		
		return result;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		boolean result;
		
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("enable_back_button", true))
			{
				result = super.onKeyDown(keyCode, event);
			}
			else
			{
				ToastHelper.toastOneway(this, R.string.toast_text_back_button_disabled);
				result = true;
			}
		}
		else
		{
			result = super.onKeyDown(keyCode, event);
		}
		
		return result;
	}
	
	private class ChoiceOnClickListener implements OnClickListener
	{
		private String	targetSectionId;
		
		public ChoiceOnClickListener(String targetSectionId)
		{
			this.targetSectionId = targetSectionId;
		}
		
		public void onClick(View v)
		{
			StaticStorage.CurrentSectionId = targetSectionId;
			startActivity(new Intent(SectionActivity.this, SectionActivity.class));
		}
	}
}
